﻿function getChargeState() {
    $.ajax({
        url: "/state",
        dataType: 'json',
        type: 'GET',
        contentType: 'application/json; charset=utf-8',
        success: function (response) {
            if (response.Connected == true) {
                $('#linearGaugeInverterPower').jqxLinearGauge('value', response.InverterPower);
                $('#linearGaugeChargerPower').jqxLinearGauge('value', response.GeneratorPower);
                $('#linearGaugeBatteryVoltage').jqxLinearGauge('value', response.BatteryVolts);
                $('#linearGaugeBatteryCurrent').jqxLinearGauge('value', response.BatteryAmps);
                $('#linearGaugePVVoltage').jqxLinearGauge('value', response.PvVolts);
                $('#linearGaugePVCurrent').jqxLinearGauge('value', response.PvAmps);
                $('#gaugeContainer').jqxGauge('value', response.Watts);

                $('#centerGaugeValue').text(response.Watts + ' Watts');
                $("div#batteryVoltGaugeValue").text(response.BatteryVolts + ' Volts');
                $("div#batteryCurrentGaugeValue").text(response.BatteryAmps + ' Amps');
                $('div#pvVoltGaugeValue').text(response.PvVolts + ' Volts');
                $('div#pvCurrentGaugeValue').text(response.PvAmps + ' Amps');
                $('div#energyToday').text('Energy today: ' + response.EnergyToday + ' kWh');

                $("div#stateText").text(response.State);
                $("div#controllerInfoText").text(response.ControllerError);
                $("div#inverterInfoText").text(response.InverterError);
                $('div#inverterPowerGaugeValue').text('AC load ' + response.InverterPower + ' Watts');
                $('div#chargerPowerGaugeValue').text('Generator ' + response.GeneratorPower + ' Watts');
                if (response.ShowInverter == true) {
                    $('#inverter').show();
                    $('#inverterInfoText').show();
                } else {
                    $('#inverter').hide();
                    $('#inverterInfoText').hide();
                }
                if (response.TotalKwH != "") {
                    $('div#totalEnergy').show();
                    $('div#totalEnergy').text('Total Energy: ' + response.TotalKwH + ' kWh');
                } else {
                    $('div#totalEnergy').hide();
                }
            } else {
                $("div#controllerInfoText").text("No Connection");
                $('#linearGaugeInverterPower').jqxLinearGauge('value', "");
                $('#linearGaugeChargerPower').jqxLinearGauge('value', "");
                $('#linearGaugeBatteryVoltage').jqxLinearGauge('value', "");
                $('#linearGaugeBatteryCurrent').jqxLinearGauge('value', "");
                $('#linearGaugePVVoltage').jqxLinearGauge('value', "");
                $('#linearGaugePVCurrent').jqxLinearGauge('value', "");
                $('#gaugeContainer').jqxGauge('value', "");

                $('#centerGaugeValue').text("");
                $("div#batteryVoltGaugeValue").text("");
                $("div#batteryCurrentGaugeValue").text("");
                $('div#pvVoltGaugeValue').text("");
                $('div#pvCurrentGaugeValue').text("");
                $('div#energyToday').text("");

                $("div#stateText").text("");
                $("div#inverterInfoText").text("");
                $('div#inverterPowerGaugeValue').text("");
                $('div#chargerPowerGaugeValue').text("");
                $('div#totalEnergy').text("");
                $('#inverterInfoText').text("");
            }
            if (response.RequestDate == true) {
                SetDate();
            }
        }
    });
};

function SetDate() {
    var now = new Date();
    $.ajax({
        url: "/state",
        dataType: 'json',
        data: { year: now.getFullYear(),
            month: now.getMonth() + 1,
            day: now.getDate(),
            hour: now.getHours(),
            minute: now.getMinutes()
        },
        type: 'POST',
        contentType: 'application/json; charset=utf-8',
        success: function (response) {
        }
    });
};

function getScale() {
    $.ajax({
        url: "/scale",
        dataType: 'json',
        type: 'GET',
        contentType: 'application/json; charset=utf-8',
        success: function (response) {
            var maxPVVoltage = response.MaxPVVoltage; /* PV VOC max */
            var powerScale = response.PowerScale; /* kW */
            var batteryVoltageScale = response.BatteryVoltageScale; /* max EQ volts */
            loadGauges(maxPVVoltage, powerScale, batteryVoltageScale);
        },
        error: function (){
            loadGauges(200, 1, 40);
        }
    });
};

function loadGauges(maxPVVoltage, powerScale, batteryVoltageScale) {
    var pvCurrentScale = Math.ceil(powerScale * 1000 / maxPVVoltage) * 2;
    var batteryCurrentScale = Math.ceil((powerScale * 1000) / batteryVoltageScale) * 2;
    $('#gaugeContainer').jqxGauge({
        ranges: [{ startValue: 0, endValue: 200 * powerScale, style: { fill: 'grey', stroke: 'grey' }, endWidth: 4, startWidth: 1 },
                        { startValue: 200 * powerScale, endValue: 400 * powerScale, style: { fill: '#fbd109', stroke: '#fbd109' }, endWidth: 8, startWidth: 4 },
                        { startValue: 400 * powerScale, endValue: 600 * powerScale, style: { fill: '#4bb648', stroke: '#4bb648' }, endWidth: 12, startWidth: 8 },
                        { startValue: 600 * powerScale, endValue: 800 * powerScale, style: { fill: '#ff8000', stroke: '#ff8000' }, endWidth: 16, startWidth: 12 },
                        { startValue: 800 * powerScale, endValue: 1000 * powerScale, style: { fill: '#e02629', stroke: '#e02629' }, endWidth: 20, startWidth: 16}],
        ticksMinor: { interval: 100 * powerScale, size: '5%' },
        ticksMajor: { interval: 200 * powerScale, size: '9%' },
        startAngle: 60,
        endAngle: 360,
        min: 0, max: 1000 * powerScale,
        value: 0,
        labels: { visible: true, interval: 50 * powerScale, position: 'outside' },
        colorScheme: 'scheme05',
        animationDuration: 0
    });

    $('#linearGaugePVVoltage').jqxLinearGauge({
        orientation: 'horizontal',
        width: 200,
        height: 60,
        ticksMajor: { size: '10%', interval: maxPVVoltage / 10 },
        ticksMinor: { size: '5%', interval: maxPVVoltage / 20, style: { 'stroke-width': 1, stroke: '#aaaaaa'} },
        min: 0, max: maxPVVoltage,
        pointer: { size: '5%' },
        colorScheme: 'scheme01',
        labels: { position: 'near', interval: maxPVVoltage / 5, offset: 4, visible: true },
        animationDuration: 0
    });

    $('#linearGaugePVCurrent').jqxLinearGauge({
        orientation: 'horizontal',
        width: 200,
        height: 60,
        ticksMajor: { size: '10%', interval: pvCurrentScale / 10 },
        ticksMinor: { size: '5%', interval: pvCurrentScale / 20, style: { 'stroke-width': 1, stroke: '#aaaaaa'} },
        min: 0, max: pvCurrentScale,
        pointer: { size: '5%' },
        colorScheme: 'scheme01',
        labels: { position: 'near', interval: pvCurrentScale / 4, offset: 4, visible: true },
        animationDuration: 0
    });

    $('#linearGaugeInverterPower').jqxLinearGauge({
        orientation: 'horizontal',
        width: 200,
        height: 60,
        ticksMajor: { size: '10%', interval: 500 },
        ticksMinor: { size: '5%', interval: 100, style: { 'stroke-width': 1, stroke: '#aaaaaa'} },
        min: 0, max: 3500,
        pointer: { size: '5%' },
        colorScheme: 'scheme01',
        labels: { position: 'near', interval: 1000, offset: 4, visible: true },
        animationDuration: 0
    });

    $('#linearGaugeChargerPower').jqxLinearGauge({
        orientation: 'horizontal',
        width: 200,
        height: 60,
        ticksMajor: { size: '10%', interval: 400 },
        ticksMinor: { size: '5%', interval: 100, style: { 'stroke-width': 1, stroke: '#aaaaaa'} },
        min: 0, max: 2500,
        pointer: { size: '5%' },
        colorScheme: 'scheme01',
        labels: { position: 'near', interval: 1000, offset: 4, visible: true },
        animationDuration: 0
    });

    $('#linearGaugeBatteryVoltage').jqxLinearGauge({
        orientation: 'horizontal',
        width: 200,
        height: 60,
        ticksMajor: { size: '10%', interval: batteryVoltageScale / 10 },
        ticksMinor: { size: '5%', interval: batteryVoltageScale / 20, style: { 'stroke-width': 1, stroke: '#aaaaaa'} },
        min: 0, max: batteryVoltageScale,
        pointer: { size: '5%' },
        colorScheme: 'scheme01',
        labels: { position: 'near', interval: batteryVoltageScale / 5, offset: 4, visible: true },
        animationDuration: 0
    });

    $('#linearGaugeBatteryCurrent').jqxLinearGauge({
        orientation: 'horizontal',
        width: 200,
        height: 60,
        ticksMajor: { size: '10%', interval: batteryCurrentScale / 10 },
        ticksMinor: { size: '5%', interval: batteryCurrentScale / 20, style: { 'stroke-width': 1, stroke: '#aaaaaa'} },
        min: 0, max: batteryCurrentScale,
        pointer: { size: '5%' },
        colorScheme: 'scheme01',
        labels: { position: 'near', interval: batteryCurrentScale / 4, offset: 4, visible: true },
        animationDuration: 0
    });

}


$(document).ready(function () {
    getScale();

    $('#centerGaugeValue').text('0 Watts');
    getChargeState();
    window.setInterval(getChargeState, 10000);
});